package com.xiansi.controller;

import com.github.pagehelper.PageInfo;
import com.xiansi.pojo.Game;
import com.xiansi.pojo.Team;
import com.xiansi.service.GameService;
import com.xiansi.vo.QueryGameVO;
import com.xiansi.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("game")
@ResponseBody
public class GameController {

    @Resource
    private GameService gameService;

    /**
     * 分页查询信息
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Game> queryByPage(Integer pageNum, Integer pageSize, QueryGameVO vo) {

        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 1;
        }
        PageInfo<Game> gamePageInfo = gameService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(gamePageInfo);
    }

    /**
     * 新增赛程
     *
     * @param game
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Game> addGame(Game game) {
        Integer i = gameService.addGame(game);
        if (i == 1) {
            return new ResultVO<Game>();
        }
        return new ResultVO<Game>(500, "请稍后再试");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Game> queryGameById(@PathVariable("id") Integer id) {
        Game game = gameService.queryById(id);
        return new ResultVO<Game>(game);
    }

    /**
     * 更新赛程
     *
     * @param game
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<Game> updateGame(@PathVariable("id") Integer gameId, Game game) {
        game.setGameId(gameId);
        Integer i = gameService.updateGame(game);
        if (i == 1) {
            return new ResultVO<Game>();
        }
        return new ResultVO<Game>(500, "请稍后再试");
    }

    /**
     * 根据id删除赛程
     *
     * @param gameId
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Game> deleteGame(@PathVariable("id") Integer gameId) {
        Integer i = gameService.deleteGame(gameId);
        if (i == 1) {
            return new ResultVO<Game>();
        }
        return new ResultVO<Game>(500, "请稍后再试");
    }
}
